<template>
	<el-dialog v-model="dialog.visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="项目名" prop="name">
				<el-input v-model="dataForm.name" placeholder="项目名"></el-input>
			</el-form-item>
			<el-form-item label="项目标识" prop="code">
				<el-input v-model="dataForm.code" placeholder="项目标识"></el-input>
			</el-form-item>
			<el-form-item label="项目包名" prop="packageName">
				<el-input v-model="dataForm.packageName" placeholder="项目包名"></el-input>
			</el-form-item>
			<el-form-item label="项目路径" prop="path">
				<el-input v-model="dataForm.path" placeholder="项目路径"></el-input>
			</el-form-item>
      <el-form-item label="属性组" prop="path">
        <el-select v-model="dataForm.propGroupId" style="width: 100%" placeholder="请选择属性组">
          <el-option v-for="ds in state.propGroupList" :key="ds.value" :label="ds.label" :value="ds.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="默认" prop="selected">
        <el-radio-group v-model="dataForm.selected" class="ml-4">
          <el-radio :label="true" size="large">是</el-radio>
          <el-radio :label="false" size="large">否</el-radio>
        </el-radio-group>
      </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="dialog.visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useProjectApi, useProjectSubmitApi } from '@/api/project'
import {IProjectForm} from "@/types/project";
import {IDialog, Option} from "@/types/common";
import {usePropGroupOptionsApi} from "@/api/prop";

const emit = defineEmits(['refreshDataList'])

// const visible = ref(false)
const dataFormRef = ref()

const dataForm: IProjectForm = reactive({
	id: '',
	name: '',
  code: '',
	packageName: '',
	path: '',
  propGroupId: '',
  selected: false
})

const dialog: IDialog = reactive({
  title: '',
  visible: false
})

const state = reactive({
  propGroupList: [] as Option[]
})

const init = (id?: number) => {
  dialog.visible = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getProject(id)
	}

  // 查询全部的属性组
  usePropGroupOptionsApi().then(res => {
    state.propGroupList = res.data
  })
}

const getProject = (id: number) => {
	useProjectApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({
	name: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	className: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	packageName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	path: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useProjectSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
          dialog.visible = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
